////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CBaseObject.h
//
//		Author			:		Vindicated Games
//
//		Purpose			:		Header to contain all generic functionality of the BaseObject and its childeren
//
//		Dates			:		Written( Feb 13, 2011 ) by Vindicated Games		LastUpdate( Feb 13, 2011  ) by Vindicated Games 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CBaseObject.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../managers/VIN_SoundManager.h"
#include "../Core/MessageSystem.h"


CBaseObject::CBaseObject()
{
	m_uiRefCount = 1;
	poisoned = false;
	m_VecPosition.fX = (0);
	m_VecPosition.fY = (0);

	m_VecVelocity.fX = (0);
	m_VecVelocity.fY = (0);

	m_nHP = 0;
	m_nWidth = 0;
	m_nHeight = 0;

	m_nType = 0;

	m_nImageID = -1;

	heightFromGround = rand()%100;
	landed = false;

	//color?
}


CBaseObject::~CBaseObject()
{

}


RECT CBaseObject::GetRect()
{
	RECT TempRect;

	SetRect( &TempRect, (int)GetPosX(), (int)GetPosY(), (int)GetPosX() + GetWidth(), (int)GetPosY() + GetHeight() );

	return TempRect;
}


bool CBaseObject::CheckCollision( IBaseInterface* pBase )
{
	RECT TempRect;

	if(IntersectRect( &TempRect, &this->GetRect(),&pBase->GetRect()))
		return true;

	return false;
}

void CBaseObject::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();

	pTM->Draw( GetImageID(), (int)GetPosX(), (int)GetPosY() );
}

void CBaseObject::Update(float fElapsedTime )
{
	/*if(m_VecVelocity.fX >= 200)
	{
		m_VecVelocity.fY = (200);
	}
	else if(m_VecVelocity.fX <= -200)
	{
		m_VecVelocity.fX = (-200); 
	}*/

	if(m_VecVelocity.fX >= 100)
	{
		m_VecVelocity.fX = (100);
	}
	else if(m_VecVelocity.fY <= -100)
	{
		m_VecVelocity.fY = (-100); 
	}

	m_VecVelocity.fY = ( m_VecVelocity.fY - (-150.0f * fElapsedTime));

	SetPosX( GetPosX() + ( GetVelX() * fElapsedTime ));
	SetPosY( GetPosY() + ( GetVelY() * fElapsedTime ));

	if(GetPosY() >= (400 + heightFromGround))
	{
		SetPosY(400 + (float)heightFromGround);
		if(!landed)
		{

			SetHP(GetHP() - (GetVelY() * .07f));
			landed = true;
			hurtcolor();
			VIN_SoundManager::GetInstance()->PlayHurt();

		}
		if(GetHP() <= 0)
		{
			SetHP(0);
			SetVelX(0);
			SetVelY(0);

		}
		SetVelY(0);
		
	}
}

bool CBaseObject::Input()
{
	return true;
}

void CBaseObject::hurtcolor()
{
	color = D3DCOLOR_XRGB(255,0,0);
	int offsetY = 0;
	offsetY = rand()%GetHeight();
	int offsetX = 0;
	offsetX = rand()&GetWidth();
	MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(GetPosX() + (((float)offsetX*0.5f)), GetPosY() - (((float)offsetY*0.5f)), 0, 0, 0));	
}